package com.example.demo.service.impl;

import com.example.demo.dao.PermissionDao;
import com.example.demo.entity.Permission;
import com.example.demo.entity.SysRole;
import com.example.demo.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Service
@Transactional(rollbackOn = Exception.class )
public class PermissionServiceImpl  implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;
    @Override
    public List<Permission> findByRoleId(Integer roleId) {
         Permission per =new Permission();
         per.setRoleId(roleId);
        Example<Permission> example =Example.of(per);
        return permissionDao.findAll(example);
    }

    /**
     * 保存所有的实体类
     * @param permissions
     */
    @Override
    public void saveBatch(List<Permission>permissions) {
        for (Permission permission:permissions){
            System.out.println("p=="+permission.toString());
            permissionDao.save(permission);
        }
    }

    @Override
    public void savePermission(Permission permission) {
        permissionDao.save(permission);
    }

    @Override
    public void deleteByRoleId(Integer roleId) {
        System.out.println("roleId==111"+roleId);
        List<Permission>permiss = findByRoleId(roleId);
        permissionDao.deleteAll(permiss);
    }
}
